'use strict';
const db = uniCloud.database()
const dbCmd = db.command
exports.main = async (event, context) => {
	const collection = db.collection('user')
	switch (event.url) {
		case 'getList':
			var total = await collection.where({
				username: new RegExp(event.username),
				delete_time: dbCmd.eq(0)
			}).count()
			var res = await collection.where({
				username: new RegExp(event.username),
				delete_time: dbCmd.eq(0)
			}).skip((event.page - 1) * event.limit).limit(event.limit).get()
			return {
				total: total.total,
				data: res.data,
				code: 200,
				msg: '操作成功'
			}
		case 'getOne':
			var res = await collection.doc(event.id).get()
			return {
				data: res.data[0],
				code: 200,
				msg: '请求成功'
			}
		case 'add':
			delete event['url']
			await collection.add(event)
			return {
				code: 200,
				msg: '操作成功'
			}
		case 'update':
			delete event['url']
			if (event.id instanceof Array) {
				for (let i = 0; i < event.id.length; i++) {
					await collection.doc(event.id[i]).update(event)
				}
			} else {
				await collection.doc(event.id).update(event)
			}
			return {
				code: 200,
				msg: '操作成功'
			}
	}

};
